//
//  PinYin5Objc.m
//  CreditAddressBook
//
//  Created by Lee on 15/8/13.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "PinYin5Objc.h"
#import "PinYin4Objc.h"

@implementation PinYin5Objc

/**
 *  转换为拼音
 */
+ (NSString *)chineseConvertToPinYin:(NSString*)chinese {
    HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
    [outputFormat setToneType:ToneTypeWithoutTone];
    [outputFormat setVCharType:VCharTypeWithV];
    [outputFormat setCaseType:CaseTypeLowercase];
    
    chinese = [chinese stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    NSString *chinesePinYin = nil;
    //异步
//    [PinyinHelper toHanyuPinyinStringWithNSString:chinese withHanyuPinyinOutputFormat:outputFormat withNSString:@" " outputBlock:^(NSString *pinYin) {
//        chinesePinYin = pinYin;
//    }];
    
    //同步
    chinesePinYin = [PinyinHelper toHanyuPinyinStringWithNSString:chinese withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
    
    return [chinesePinYin uppercaseString];
}

/**
 *  汉字数组转换为拼音数组
 */
+ (NSArray *)chineseArrayConvertToPinYinArray:(NSArray *)chineseArray {
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in chineseArray) {
        if (str && str.length) {
            [arr addObject:[self chineseConvertToPinYin:str]];
        }
    }
    return arr;
}

/**
 *  获取汉字或英文的拼音首字母
 */
+ (NSString *)chineseConvertToPinYinHead:(NSString *)chinese {
    NSString *pinyin = [self chineseConvertToPinYin:chinese];
    
    if (pinyin.length > 1) {
        pinyin = [pinyin substringToIndex:1];
    }
    
    return [pinyin uppercaseString];
}

/**
 *  获取汉字或英文数组的首字母数组
 */
+ (NSArray *)chineseArrayConvertToPinYinHeadArray:(NSArray *)chineseArray {
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in chineseArray) {
        if (str && str.length) {
            [arr addObject:[self chineseConvertToPinYinHead:str]];
        }
    }
    return arr;
}

/**
 *  获取字符串中每个汉字或英文的拼音首字母字符串
 */
+ (NSString *)chineseConvertToPinYinHeads:(NSString *)chinese {
    if (!chinese) return nil;
    NSString *str = [self chineseConvertToPinYin:chinese];
    NSArray *arr = [str componentsSeparatedByString:@" "];
    
    NSString *head = @"";
    for (NSString *str in arr) {
        if (str && str.length) {
            head = [head stringByAppendingString:[str substringToIndex:1]];
        }
    }
    return [head uppercaseString];
}

/**
 *  获取字符串数组中每个汉字或英文的拼音首字母字符串数组
 */
+ (NSArray *)chineseArrayConvertToPinYinHeads:(NSArray *)chineseArray {
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in chineseArray) {
        if (str && str.length) {
            [arr addObject:[self chineseConvertToPinYinHeads:str]];
        }
    }
    return arr;
}
@end
